Median Filter is also known as Non linear filter and it is also Edge preserving.
In [1]:
import cv2
import matplotlib.pyplot as plt
import skimage.util as ski
import scipy.ndimage.filters as sci
In [2]:
image = cv2.imread('demo1.jpg', cv2.IMREAD_GRAYSCALE)
In [3]:
SnP_image = ski.random_noise(image, mode='s&p', seed=128)
plt.figure(figsize=(15,12))
plt.subplot(1,2,1), plt.imshow(image, cmap='gray'), plt.title('Original')
plt.subplot(1,2,2), plt.imshow(SnP_image, cmap='gray'), plt.title('S&P')
plt.show()
In [4]:
Median_filtered = sci.median_filter(SnP_image, 3)
plt.figure(figsize=(20,18))
plt.subplot(1,3,1), plt.imshow(SnP_image, cmap='gray'), plt.title('Salt and pepper noised image')
plt.subplot(1,3,2), plt.imshow(Median_filtered, cmap='gray'), plt.title('Median Filtered')
plt.subplot(1,3,3), plt.imshow(image, cmap='gray'), plt.title('Original')
plt.show()